﻿(function () {
	church.ajax =
	{
		load: function (url, dataParam, callbackParam) {
			var target = this;
			church.ajax.post(url, dataParam, function (data) {
				$(target).html(data);
				var form = target.find("form");
				if (form != undefined) {
					$(form).removeData("validator");
					$(form).removeData("unobtrusiveValidation");
					$.validator.unobtrusive.parse($(form));
				}
				if ($(data).find("div.validation-summary-errors").length > 0)
					return;
				if (typeof (callbackParam) == 'function')
					callbackParam(data);
			});
		},

		post: function (url, dataParam, callbackParam, sender) {
			church.ajax.send(url, dataParam, callbackParam, sender);
		},

		get: function (url, dataParam, callbackParam, sender) {
			church.ajax.send(url, dataParam, callbackParam, sender);
		},

		send: function (url, dataParam, callbackParam, sender) {
			var defaultDataType = 'html';
			var defaultMethod = jQuery.post;
			var paramObj = typeof (dataParam) == 'object' ? dataParam : {};
			var callBackFunc = typeof (callbackParam) == 'function' ? callbackParam : (typeof (dataParam) == 'function' ? dataParam : null);
			var dataType = typeof (callbackParam) == 'string' ? callbackParam : defaultDataType;
			window.status = "Please wait...";
			document.body.style.cursor = "wait";
			defaultMethod(url, paramObj, function (data, textStatus, jqxhr) {
				window.status = "Done";
				document.body.style.cursor = "default";
				if (data != 'Unauthorized') {
					var status = "";
					if (dataType == 'json')
						data = jQuery.parseJSON(data);

					var message = "";
					var title = "Error";
					try {
						if (data.indexOf('{"Status') == 0) {
							var res = jQuery.parseJSON(data);
							if (res.Status != "undefined")
								status = res.Status;
							if (res.Message != "undefined")
								message = res.Message;
							if (res.Title != "undefined")
								title = res.Title;
						}
					}
					catch (e) { }

					if (status == "Failed") {
						if (sender) {
							var vs = $(sender).find("div[class*=validation-summary]");
							if (vs.length > 0) {
								vs.addClass("validation-summary-errors");
								vs.removeClass("validation-summary-valid");
								var ul = vs.find("ul");
								if (ul.length > 0) {
									ul.html("");
									ul.append("<li>" + message + "</li>");
								} else {
									vs.html("");
									vs.append("<ul></ul>");
									ul.append("<li>" + message + "</li>");
								}
							}
							else
								alertMessage(title, message);
						} else
							alertMessage(title, message);
					} else if (callBackFunc != null)
						callBackFunc(data, textStatus, jqxhr);
				}
				else
					alertMessage("Error", "Unauthorized");
			}, 'html');

			function alertMessage(title, message) {
				var alertOverlay = $("#alertOverlay");
				$(alertOverlay).alertMessage(window.alertUrl, title, message, function () { $(sender).alert(); $(sender).toggle(); setTimeout(function () { $(sender).toggle(); }, 5000); });
			};
		}
	};
})();

